package net.caixiaomi.info.Lottery.presenter;

import net.caixiaomi.info.Lottery.callback.BallCallBack;
import net.caixiaomi.info.Lottery.callback.LotteryMainCallBack;
import net.caixiaomi.info.Lottery.model.BallItem;
import net.caixiaomi.info.Lottery.model.LotteryBettingEntity;
import net.caixiaomi.info.Lottery.view.HorizontalBallView;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by suntian on 2018/8/16.
 */

public class LotteryEasyPresenter implements BallCallBack {

    private HorizontalBallView ballViewBefore;
    private HorizontalBallView ballViewAfter;
    private LotteryMainCallBack mCallBack;
    private int mStandardChips;
    private long mStandardMoney;
    private int price=2; // 单注价格

    public void initStandardView(HorizontalBallView ballViewBefore, HorizontalBallView ballViewAfter, LotteryMainCallBack callback){
        this.ballViewBefore=ballViewBefore;
        this.ballViewAfter=ballViewAfter;
        mCallBack=callback;
    }

    @Override
    public boolean gonChangeBallData(int type,BallItem ball) {
        return onBallChanged(type,ball);
    }

    public void addBettingData(){
        CommonApp.mLotteryBettingList.clear();
        LotteryBettingEntity entity=getData();
        if(CommonApp.mLotteryIndex!=-1){
            CommonApp.mLotteryBettingList.remove(CommonApp.mLotteryIndex);
            CommonApp.mLotteryBettingList.add(CommonApp.mLotteryIndex,entity);
        }else{
            CommonApp.mLotteryBettingList.add(entity);
        }
        String aa="";
    }


    /*
    * 即时更新投注信息
    */
    public boolean onBallChanged(int type,BallItem ball){
        int statusTtotalChips = 0;
        int frontDanCount = 0; // 前区胆码数据
        int frontTuoCount = 0;
        int backDanCount = 0;
        int backTuoCount = 0;
        int frontChips = 0;
        int backChips = 0;
            for(int i=0;i<ballViewBefore.getBallData().size();i++){
                if(ballViewBefore.getBallData().get(i).isSelect()){
                    frontTuoCount++;
                }
            }
            for(int i=0;i<ballViewAfter.getBallData().size();i++){
                if(ballViewAfter.getBallData().get(i).isSelect()){
                    backTuoCount++;
                }
            }
            if(frontTuoCount>18){
                ToastUtil.s("最多只能选择18个红球");
                return false;
            }

        if (((frontDanCount + frontTuoCount) == 5 && frontDanCount > 0)
                || ((backDanCount + backTuoCount) == 2 && backDanCount > 0)) {
            statusTtotalChips = 0;
        } else {
            switch (frontDanCount) {
                case 4:// 选4胆
                    frontChips = frontTuoCount;
                    break;
                case 3:// 选3胆
                    frontChips = frontTuoCount * (frontTuoCount - 1) / (1 * 2);
                    break;
                case 2:// 选2胆
                    frontChips = frontTuoCount * (frontTuoCount - 1)
                            * (frontTuoCount - 2) / (1 * 2 * 3);
                    break;
                case 1:// 选1胆
                    frontChips = frontTuoCount * (frontTuoCount - 1)
                            * (frontTuoCount - 2) * (frontTuoCount - 3)
                            / (1 * 2 * 3 * 4);
                    break;
                case 0:// 选0胆
                    frontChips = frontTuoCount * (frontTuoCount - 1)
                            * (frontTuoCount - 2) * (frontTuoCount - 3)
                            * (frontTuoCount - 4) / (1 * 2 * 3 * 4 * 5);
                    break;
            }
            switch (backDanCount) {
                case 1:// 选1胆
                    backChips = backTuoCount;
                    break;
                case 0:// 选0胆
                    backChips = backTuoCount * (backTuoCount - 1) / (1 * 2);
                    break;
            }
            if (frontChips * backChips >= 1) {
                statusTtotalChips = frontChips * backChips;
            }
        }

        long tempBetMoney = (long)   statusTtotalChips * price;
        int tempTotalChips = statusTtotalChips;

        if(tempBetMoney>20000){
            ToastUtil.s("单次投注最多2万元");
            return false;
        }

        String des="";
        boolean correct;
        if(statusTtotalChips==0){
            des="<html><body>请至少选择<font color='red'>5</font>个红球 <font color='blue'>2</font>个蓝球 </body></html>";
            correct=false;
        }else{
//            des="共"+tempTotalChips+"注  合计"+tempBetMoney+"元";
            des="<html><body>共<font color='red'>"+tempTotalChips+"</font>注  合计<font color='red'>"+tempBetMoney+"</font>元</body></html>";
            correct=true;
        }
        mCallBack.getkTotalDes(des,correct);

        mStandardChips=tempTotalChips;
        mStandardMoney=tempBetMoney;

        return true;
    }

    private LotteryBettingEntity getData(){
        LotteryBettingEntity entity=new LotteryBettingEntity();
            List<String> befores=new ArrayList<>();
            List<String> afters=new ArrayList<>();
            for(int i=0;i<ballViewBefore.getBallData().size();i++){
                BallItem item=ballViewBefore.getBallData().get(i);
                if(item.isSelect()){
                    befores.add(item.getNum()<10?"0"+item.getNum():""+item.getNum());
                }
            }
            for(int i=0;i<ballViewAfter.getBallData().size();i++){
                BallItem item=ballViewAfter.getBallData().get(i);
                if(item.isSelect()){
                    afters.add(item.getNum()<10?"0"+item.getNum():""+item.getNum());
                }
            }
            entity.setmBefores(befores);
            entity.setmAfters(afters);
            if(befores.size()==5&&afters.size()==2){
                entity.setStatus(0);
            }else{
                entity.setStatus(1);
            }
            entity.setChips(mStandardChips);
            entity.setMoney(mStandardMoney);
        return entity;
    }

    public boolean isNoBallCheck(){
            for(int i=0;i<ballViewBefore.getBallData().size();i++){
                if(ballViewBefore.getBallData().get(i).isSelect()){
                    return false;
                }
            }
            for(int i=0;i<ballViewAfter.getBallData().size();i++){
                if(ballViewAfter.getBallData().get(i).isSelect()){
                    return false;
                }
            }
        return true;
    }
}
